package com.hdoit.apollo.web.stock.apis;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.inventory.model.InventoryShow;
import com.hdoit.apollo.logical.inventory.model.InventoryStaff;
import com.hdoit.apollo.logical.inventory.service.IInventoryStaffService;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.model.StockCollocation;
import com.hdoit.apollo.logical.stock.model.StockStat;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.stock.StockSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.data.message.PagerModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/api/v1/stock/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/stock")
public class StockApi extends BaseApi{

	@Autowired
	private IStockService stockService;
	
	@Autowired
	private IStockCollocationService stockCollocationService;
	
	@Autowired
	private IInventoryStaffService inventoryStaffService;
	
	private final String getStocks="getStocks";
	private final String getStockStat="getStockStat";
	private final String getStocksByScrollApi="getStocksByScroll";
	private final String getStockCollocationByCargoIdApi="getStockCollocationByCargoId";
	private final String getStockByCargoNo="getStockByCargoNo";
	private final String getStockByInventoryStaffId="getStockByInventoryStaffId";
	
    @RequestMapping(value = getStockByInventoryStaffId)
    @ApiOperation(notes = getStockByInventoryStaffId, httpMethod = "GET", value = "通过inventoryStaffId获取货物信库存信息")
    public @ResponseBody String getStockByInventoryStaffId(String inventoryStaffId,String shopId){
    	InventoryStaff inventoryStaff= inventoryStaffService.queryModelById(inventoryStaffId);
    	List<Stock> stocks=new ArrayList<Stock>();
    	if(inventoryStaff!=null) {
    		for(InventoryShow inventoryShow:inventoryStaff.getInventoryShows()) {
        		Stock stock=stockService.getStockByCargoId(inventoryShow.getCargoId(),shopId);
        		stocks.add(stock);
        	}
    	}
    	ResponseModel rm=new ResponseModel();
    	rm.put("stocks",stocks);
    	return rm.toJson();
    }
	
    @RequestMapping(value = getStockByCargoNo)
    @ApiOperation(notes = getStockByCargoNo, httpMethod = "GET", value = "通过cargoNo获取货物信库存信息")
    public @ResponseBody String getStockByCargoNo(String cargoNo,String shopId){
    	Stock stock=stockService.getStockByCargoNo(cargoNo,shopId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("stock",stock);
    	return rm.toJson();
    }
	 
    @RequestMapping(value = getStocks)
    @ApiOperation(notes = getStocks, httpMethod = "GET", value = "获取库存列表")
    public @ResponseBody String getGroupInfo(StockSearch search){
    	List<Stock> stocks = stockService.queryListMP(search,null);
    	ResponseModel rm=new ResponseModel();
    	rm.put("stocks",stocks);
    	return rm.toJson();
    }
	 
    @RequestMapping(value = getStockStat)
    @ApiOperation(notes = getStockStat, httpMethod = "GET", value = "获取库存统计信息")
    public @ResponseBody String getStockStat(StockSearch search){
    	StockStat stockStat=stockService.getStockStat(search);
    	ResponseModel rm=new ResponseModel();
    	rm.put("stockStat",stockStat);
    	return rm.toJson();
    }
    
 	@RequestMapping(value = getStocksByScrollApi)
    @ApiOperation(notes = getStocksByScrollApi, httpMethod = "POST", value = "滚动查找盘点信息列表")
    public @ResponseBody String getStocksByScrollApi(StockSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}

    	List<Stock> stocks=stockService.queryListMP(search, pagerModel);
    	rm.put("stocks",stocks);
    	return rm.toJson();
    }
    
    @RequestMapping(value = getStockCollocationByCargoIdApi)
    @ApiOperation(notes = getStockCollocationByCargoIdApi, httpMethod = "GET", value = "获取规格库存列表")
    public @ResponseBody String getStockCollocationByCargoId(String cargoId){
    	List<StockCollocation> stockCollocations = stockCollocationService.getStockCollocationByCargoId(cargoId);
    	ResponseModel rm=new ResponseModel();
    	rm.put("stockCollocations",stockCollocations);
    	return rm.toJson();
    }

}
